using System.IO;
using ICommonLibrary.Resource;
using Org.BouncyCastle.Bcpg.OpenPgp;

namespace ICommonLibrary.Security {

    public class PgpDecryptor {

        public IResource SecretKeyResource { get; set; }
        public string SecretKeyPassword { get; set; }

        private readonly PgpSecretKeyRingBundle _secretKeyRingBundle;

        public PgpDecryptor(IResource secretKeyResource, string secretKeyPassword) {
            Asserts.NotNull(SecretKeyResource = secretKeyResource, "SecretKeyResource has not been specified");
            Asserts.NotNull(SecretKeyPassword = secretKeyPassword, "SecretKeyPassword has not been specified");

            _secretKeyRingBundle = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(SecretKeyResource.GetStream()));
        }

        public byte[] Decrypt(byte[] input) {
            return Decrypt(new MemoryStream(input));
        }

        public byte[] Decrypt(Stream input) {
            return Pgp.Decrypt(input, _secretKeyRingBundle, SecretKeyPassword.ToCharArray());
        }

    }
}